package jp.co.ledinge.syllabarygame;

import java.io.File;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class SyllabaryDataActivity extends Activity {
	/** 文字入力用BoxView */
	private BoxView mInputBoxView;
	/** 文字出力用BoxView */
	private BoxView mOutputBoxView;
	/** 文字インデックス */
	private int mCharIndex = 0;
	/** 入力テキスト文字 */
	private String[] mCharStrs;
	/** 入力するテキスト文字 */
	private TextView mTxtA;
	/** 結果のフィールド */
	private TextView mResultField;
	private static final String TAG = "SyllabaryData";
	/** 文字認識エンジン */
	private Recognize mRecognize;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		mInputBoxView = (BoxView) findViewById(R.id.boxView);
		mCharStrs = this.getResources().getStringArray(R.array.char_list);
		mInputBoxView.setCharCode(mCharStrs[mCharIndex]);
		mOutputBoxView = (BoxView) findViewById(R.id.boxView2);
		mTxtA = (TextView) findViewById(R.id.txt_a);
		mTxtA.setText(mCharStrs[mCharIndex]);
		mResultField = (TextView) findViewById(R.id.result);
		try {
			InputStream input;
			if (!getFileStreamPath("SaveData.dat").exists()) {
				AssetManager as = getResources().getAssets();
				input = as.open("SaveData.dat");
			} else {
				File path = getFilesDir();
				Log.d(TAG, path.getAbsolutePath());
				input = openFileInput("SaveData.dat");
			}
		    ObjectInputStream ois = new ObjectInputStream(input);
		    HashMap<String, Pattern> readObject = (HashMap<String, Pattern>) ois.readObject();
			mRecognize = new Recognize(readObject, this.getResources().getStringArray(R.array.char_list), getApplicationContext());
		    ois.close();
			input.close();
		} catch (Exception e) {
		    Log.e(TAG, "Error", e);
		}
	}

	/**
	 * 前ボタン処理
	 */
	public void onClickPrev(View view) {
		mCharIndex--;
		if (mCharIndex < 0) {
			mCharIndex = mCharStrs.length - 1;
		}
		mTxtA.setText(mCharStrs[mCharIndex]);
		mInputBoxView.setCharCode(mCharStrs[mCharIndex]);
	}
	/**
	 * 次ボタン処理
	 */
	public void onClickNext(View view) {
		mCharIndex ++;
		int max = mCharStrs.length;
		if (max <= mCharIndex) {
			mCharIndex = 0;
		}
		mTxtA.setText(mCharStrs[mCharIndex]);
		mInputBoxView.setCharCode(mCharStrs[mCharIndex]);
	}
	/**
	 * 画面のクリア
	 * 
	 * @param view
	 */
	public void onClickCls(View view) {
		mInputBoxView.clearDrawList();
		mResultField.setText("");
	}

	/**
	 * データのセーブ
	 * 
	 * @param view
	 */
	public void onClickSave(View view) {
		mRecognize.save();
		File path = getFilesDir();
		Log.d(TAG, path.getAbsolutePath());
	}

	public void onClickSampling(View view) {
		// 座標から有効点を取る
		Point[] samplePoints = mInputBoxView.samplingPoint();
		mOutputBoxView.clearDrawList();
		ArrayList<Stroke> strokes = mRecognize.changePoint2Stroke(samplePoints);
		Point[] points = mRecognize.normalizeChar(strokes);
		mOutputBoxView.drawSamplingPoint(points);
	}
	public void onClickApproximateBySraightLine(View view) {
		// 座標から有効点を取る
		Point[] samplePoints = mInputBoxView.samplingPoint();
		Point[] points = null;
		ArrayList<Stroke> strokes = mRecognize.changePoint2Stroke(samplePoints);
		points = mRecognize.normalizeChar(strokes);
		points = mRecognize.approximateByStraightLine(points);
		mOutputBoxView.clearDrawList();
		mOutputBoxView.drawSamplingPoint(points);
//		Pattern pattern = unifyShortSegment(points);
//		pattern = approximateByConstantPitch(pattern);
	}
	public void onClickUnifyShortSegments(View view) {
		// 座標から有効点を取る
		Point[] samplePoints = mInputBoxView.samplingPoint();
		Point[] points = null;
		ArrayList<Stroke> strokes = mRecognize.changePoint2Stroke(samplePoints);
		points = mRecognize.normalizeChar(strokes);
		points = mRecognize.approximateByStraightLine(points);
		Pattern pattern = mRecognize.unifyShortSegment(points);
		pattern = mRecognize.approximateByConstantPitch(pattern);
		mOutputBoxView.clearDrawList();
		mOutputBoxView.drawPattern(pattern);
	}
	public void onClickRegist(View view) {
		// 座標から有効点を取る
		Point[] samplePoints = mInputBoxView.samplingPoint();
		Point[] points = null;
		ArrayList<Stroke> strokes = mRecognize.changePoint2Stroke(samplePoints);
		points = mRecognize.normalizeChar(strokes);
		points = mRecognize.approximateByStraightLine(points);
		Pattern pattern = mRecognize.unifyShortSegment(points);
		pattern = mRecognize.approximateByConstantPitch(pattern);
		mOutputBoxView.clearDrawList();
		mOutputBoxView.drawPattern(pattern);
		String nowStr = mCharStrs[mCharIndex];
		pattern.setCharStr(nowStr);
		mRecognize.regist(nowStr, pattern);
		
	}
	
	public void onClickRecognize(View view) {
		// 座標から有効点を取る
		Point[] samplePoints = mInputBoxView.samplingPoint();
		Point[] points = null;
		ArrayList<Pattern> patternList = new ArrayList<Pattern>();
		if (samplePoints.length == 0) {
			return;
		}
		ArrayList<Stroke> strokes = mRecognize.changePoint2Stroke(samplePoints);
		points = mRecognize.normalizeChar(strokes);
		points = mRecognize.approximateByStraightLine(points);
		Pattern inPattern = mRecognize.unifyShortSegment(points);
		inPattern = mRecognize.approximateByConstantPitch(inPattern);
		mOutputBoxView.clearDrawList();
		mOutputBoxView.drawPattern(inPattern);
		
		patternList = mRecognize.recognize(mInputBoxView);
	    StringBuilder result = new StringBuilder();
	    for (Pattern outPattern: patternList) {
	    	result.append(outPattern.getCharStr());
	    	result.append(":");
	    	result.append(outPattern.getDistance());
	    	result.append("\n");
	    }
	    mResultField.setText(result.toString());
	}
}